﻿using OpenIddict.Abstractions;
using System.Globalization;
using Volo.Abp.DependencyInjection;

namespace Demo.AuthService.Services
{

    /// <summary>
    /// Scpe管理类
    /// </summary>
    [Dependency(ServiceLifetime.Singleton)]
    public class ScopesService : IScopesService
    {

        public IOpenIddictScopeManager _ScopeManager { get; set; }

        /// <summary>
        /// 创建scope方法
        /// </summary>
        /// <returns></returns>
        public async Task CreatAsync(string Name, string DisplayName)
        {
            // 微服务信息保存
            if (await _ScopeManager.FindByNameAsync(Name) == null)
            {
                await _ScopeManager.CreateAsync(new OpenIddictScopeDescriptor()
                {
                    Name = Name, // 微服务名称
                    DisplayName = DisplayName,
                    DisplayNames =
                    {
                        [CultureInfo.GetCultureInfo("zh-Hans")] = "演示 API 访问",
                        [CultureInfo.GetCultureInfo("tr")] = "API Description 。。。"
                    },
                    Resources =
                    {
                        "list",
                        "select", // 微服务名称接口,直接写接口名称
                        "delete",
                        "update",
                        "insert"
                    }
                });
            }
        }
    }
}
